﻿using System;
using System.Data;
using System.Collections;
using System.Web;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;

using ent;

public partial class main_sitemap : System.Web.UI.Page {
    public String URL = ApplicationCommon.getPath();
    public String URL_CONTENT = "~/main/content.aspx";

    private bool _permitManagement = false;

    private TableUsers t1 = new TableUsers();
    private TableContents t2 = new TableContents();

    private int _listLength = 0;
    private int _listDepth = 0;
    private int _listLevelPrevious = 1;

    protected void Page_Load(Object sender, EventArgs e) {
        // Authenticate.
        authenticateUser();
        // Get content and output as hierarchical links.
        DataTable dt1 = t2.getContentsHierarchy(null);
        _listLength = dt1.Rows.Count; // To be used on event: ItemDataBound.
        rptrSitemap.DataSource = dt1;
        rptrSitemap.DataBind();
        // Is empty.
        if(rptrSitemap.Items.Count <= 0) {
            lblError.Text = ResourceCommon.msgError_NoRecordsFound;
        }
    }

#region Methods

    /// <summary>Authenticate user.</summary>
    protected void authenticateUser() {
        if(!ApplicationCommon.isValidSession(Session)) {
            _permitManagement = false;
        } else {
            _permitManagement = true;
        }
    }

#endregion

#region Events

    protected void rptrSitemap_ItemDataBound(Object sender, RepeaterItemEventArgs e) {
        // This event is raised for the header, the footer, separators, and items.
        if(e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem) {
            DataRowView drv1 = (DataRowView)e.Item.DataItem;

            // Get controls.
            Literal litMarkup = (Literal)e.Item.FindControl("litItemMarkup");

            // Create markup for link.
            System.Text.StringBuilder sb1 = new System.Text.StringBuilder();
            Hashtable qs1 = new Hashtable();
            qs1.Add("id", drv1[TableContents.TBL__contents_hierarchy__content_id].ToString());
            String linkUrl = ResolveClientUrl(ApplicationCommon.setQueryString(qs1, URL_CONTENT));
            String linkInner = (String)drv1[TableContents.TBL__contents_hierarchy__content_title];
            sb1.AppendFormat("<a href=\"{0}\">{1}</a>", linkUrl, linkInner);
            
            // Create markup for status.
            System.Text.StringBuilder sb2 = new System.Text.StringBuilder();
            if(_permitManagement) {
                int intPageAssignedToId = (int)drv1[TableContents.TBL__contents_hierarchy__content_assignedto];
                sb2.Append("<span class=\"frmsitemap-controlpanel\"> [Assigned To: <span class=\"bold\">");
                sb2.Append(intPageAssignedToId);
                if(intPageAssignedToId > 0) {
                    sb2.Append(" ");
                    sb2.Append(t1.getUserHumanName(intPageAssignedToId));
                } else {
                    sb2.Append(" ");
                    switch(intPageAssignedToId) {
                        case (int)TableLists.enumContentAssignedTo.Administrators:
                            sb2.Append("Administrators");
                            break;
                        case (int)TableLists.enumContentAssignedTo.Moderators:
                             sb2.Append("Moderators");
                            break;
                        case (int)TableLists.enumContentAssignedTo.Authors:
                             sb2.Append("Authors");
                            break;
                        default:
                             sb2.Append("Unknown");
                            break;
                    }
                }
                 sb2.Append("</span>]</span>");
            }
            
            // Create markup code for list item.
            System.Text.StringBuilder sb3 = new System.Text.StringBuilder();
            int listLevelCurrent = (int)drv1[TableContents.TBL__contents_hierarchy__content_level];

            if(listLevelCurrent < _listLevelPrevious) {
                int listLevelDifference = _listLevelPrevious - listLevelCurrent;
                for(int i = 0;i < listLevelDifference;i++) {
                    sb3.Append("</li></ul>");
                    _listDepth--;
                }
            }
            
            if(listLevelCurrent > _listLevelPrevious) {
                sb3.Append("<ul>");
                _listDepth++;;
            } else if(rptrSitemap.Items.Count > 0) {
                sb3.Append("</li>");
            }

            sb3.Append("\n<li>");
            sb3.AppendFormat("{0}{1}", sb1, sb2);

            if(rptrSitemap.Items.Count >= (_listLength - 1)) {
                if(_listDepth > 0) {
                    for(int i = 0;i < _listDepth;i++) {
                        sb3.Append("</li></ul>");
                        _listDepth--;
                    }
                }
                sb3.Append("</li>\n");
            }

            _listLevelPrevious = listLevelCurrent;
            litMarkup.Text = sb3.ToString();
        }
    }
    protected void rptrSitemap_ItemCommand(Object sender, RepeaterCommandEventArgs e) { }

#endregion

}
